package com.deepface.framework.config;

import com.rabbitmq.client.ConnectionFactory;
import java.net.ConnectException;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName RabbitMQConfig
 * @Description 编写RabbitMQConfig配置类，并交给spring容器管理，
 *          主要是用于创建交换机和队列，并绑定关系，指定bindingkey
 * @Author 74707
 * @Date 2023/12/16 14:35
 * @Version V1.0
 */
@Configuration
public class RabbitMQConfig {
  /**
   * 声明交换机 类型是主题类型
   */
  public static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";
  /**
   * 声明QUEUE_INFORM_EMAIL队列
   */
  public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
  /**
   * 声明QUEUE_INFORM_SMS队列
   */
  public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
  /**
   * 声明LOG_QUEUE队列
   */
  public static final String LOG_QUEUE ="log_queue";
  /**
   * ROUTINGKEY_EMAIL队列绑定交换机，指定routingKey
   */
  public static final String ROUTINGKEY_EMAIL="inform.#.email.#";
  /**
   * ROUTINGKEY_SMS队列绑定交换机，指定routingKey
   */
  public static final String ROUTINGKEY_SMS="inform.#.sms.#";

  public static final String ROUTINGKEY_LOG_QUEUE="inform.#.log_queue.#";

  @Bean(EXCHANGE_TOPICS_INFORM)
  public Exchange EXCHANGE_TOPICS_INFORM(){
    //durable(true) 持久化，mq重启之后交换机还在
    return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
  }

  @Bean(QUEUE_INFORM_EMAIL)
  public Queue QUEUE_INFORM_EMAIL(){
    return new Queue(QUEUE_INFORM_EMAIL);
  }

  @Bean(QUEUE_INFORM_SMS)
  public Queue QUEUE_INFORM_SMS(){
    return new Queue(QUEUE_INFORM_SMS);
  }

  @Bean(LOG_QUEUE)
  public Queue LOG_QUEUE(){
    return new Queue(LOG_QUEUE);
  }

  @Bean
  public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
      @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
    return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_EMAIL).noargs();
  }

  @Bean
  public Binding BINDING_ROUTINGKEY_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
      @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
    return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_SMS).noargs();
  }

  @Bean
  public Binding BINDING_ROUTINGKEY_LOG_QUEUE(@Qualifier(LOG_QUEUE) Queue queue,
      @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
    return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY_LOG_QUEUE).noargs();
  }
}